package org.tabletopmachine.states.mainmenu;

import org.tabletopmachine.Game;
import org.tabletopmachine.IMenu;
import de.matthiasmann.twl.Button;
import de.matthiasmann.twl.DialogLayout;
import de.matthiasmann.twl.Label;
import de.matthiasmann.twl.Widget;

public class MainMenu extends Widget implements IMenu {

	private final DialogLayout _menuPanel;
	private final Button _newGameButton;
	private final Button _optionsButton;
	private final Button _exitButton;
	private final MainMenuState _mMS;
	private final Label _title;

	public MainMenu(MainMenuState mainMenuState) {
		_mMS = mainMenuState;
		_newGameButton = new Button("New Match");
		_optionsButton = new Button("Options");
		_exitButton = new Button("Exit");
		_menuPanel = new DialogLayout();

		_title = new Label("Welcome to The Tabletop Machine");
		_menuPanel.setHorizontalGroup(_menuPanel.createParallelGroup(_title, _newGameButton, _optionsButton, _exitButton));
		_menuPanel.setVerticalGroup(_menuPanel.createSequentialGroup(_title, _newGameButton, _optionsButton, _exitButton));
		add(_menuPanel);
	}

	public void init() {
		// _optionsButton.setEnabled(false);

		_newGameButton.addCallback(new Runnable() {

			@Override
			public void run() {
				newGame();
			}

		});

		_optionsButton.addCallback(new Runnable() {

			@Override
			public void run() {
				options();
			}

		});
		_exitButton.addCallback(new Runnable() {

			@Override
			public void run() {
				exit();
			}

		});

	}

	protected void layout() {
		setSize(900, 600);
		_menuPanel.adjustSize();
		_menuPanel.setPosition(getInnerX() + (getInnerWidth() - _menuPanel.getWidth()) / 2, getInnerY() + (getInnerHeight() - _menuPanel.getHeight()) / 2);
	}

	private void newGame() throws IllegalStateException {
		Game.initNewMatch();
		_mMS.enterNewGameMenu();
	}

	private void options() {

	}

	private void exit() {
		Game.getGameInstance().shutdown();
	}

	public void update() {

	}
}
